package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MemberSignature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

import static com.sky.constant.AutoFillConstant.*;

@Aspect
@Component
@Slf4j
public class AutoFillAspect {
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void AutoFillPointCut(){}
    @Before("AutoFillPointCut()")
    public void autoFill(JoinPoint joinPoint){
//      1.获取操作类型
//        1.1获取标签
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        1.2获取注解对象
        AutoFill annotation = signature.getMethod().getAnnotation(AutoFill.class);
//        1.3获取操作类型
        OperationType operationTypeValue = annotation.value();

//       2.获取方法的实体对象
//        2.1获取实体对象
        Object[] args = joinPoint.getArgs();
//        2.2判断实体是否为空
        if(args == null || args.length==0){
            return;//实体为空则直接返回
        }
//        2.3提取实体对象
        Object arg = args[0];

//       3.提供数据以备赋值
        LocalDateTime now = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();

//       4.按操作类型执行对应操作
//        4.1判断操作类型
//          4.1.1操作类型为INSERT
        if(operationTypeValue==OperationType.INSERT){
            try{
                Method setCreatTime = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setCreatUser = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdataTime = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdataUser = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
//                通过反射来赋值
                setCreatUser.invoke(arg,currentId);
                setCreatTime.invoke(arg,now);
                setUpdataUser.invoke(arg,currentId);
                setUpdataTime.invoke(arg,now);
            }catch (Exception e){
                e.printStackTrace();
            }

//          4.1.2操作类型为UPDATA
        }else if(operationTypeValue==OperationType.UPDATE){
            try{
                Method setUpdataTime = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdataUser = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
//                通过反射来赋值
                setUpdataUser.invoke(arg,currentId);
                setUpdataTime.invoke(arg,now);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }
}
